package com.lianniu.mgr.teaOrder.controller;

import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lianniu.mgr.base.BaseResult;
import com.lianniu.mgr.pagination.PaginationInfo;
import com.lianniu.mgr.teaOrder.model.TeaOrder;
import com.lianniu.mgr.teaOrder.service.ITeaOrderService;



@Controller
@RequestMapping("/rest/tea/order")
public class TeaOrderController {
	
	private final static Logger logger = Logger.getLogger(TeaOrderController.class);
	
	@Autowired
	private ITeaOrderService teaOrderService;
	
	/**
	 * @return
	 */
	@RequestMapping(value="/getOrderInfoBefore", method = {RequestMethod.GET, RequestMethod.POST})
	public @ResponseBody BaseResult getOrderInfoBefore(HttpServletRequest req){
		logger.info("TeaOrderController  getOrderInfoBefore begin.");
		try {
			String userId = req.getParameter("userId");
			String productId = req.getParameter("productId");
			logger.info("getOrderInfoBefore begin.");
			BaseResult baseResult = teaOrderService.getOrderInfoBefore(userId,productId);
			logger.info("getOrderInfoBefore end.");
			logger.info("TeaOrderController getOrderInfoBefore end.");
			return baseResult;
		} catch (Exception e) {
			logger.error("TeaOrderController getOrderInfoBefore error:" + e.getMessage());
			logger.error("TeaOrderController getOrderInfoBefore end.");
			return BaseResult.exception(e.getMessage());
		}
	}
	
	@RequestMapping(value="/getOrderInfo", method = RequestMethod.GET)
	public @ResponseBody BaseResult getOrderInfo(HttpServletRequest req){
		logger.info("TeaOrderController  getOrderInfo begin.");
		try {
			String orderId = req.getParameter("orderId");
			logger.info("getOrderInfo begin.");
			BaseResult baseResult = teaOrderService.getOrderInfo(orderId);
			logger.info("getOrderInfo end.");
			logger.info("TeaOrderController getOrderInfo end.");
			return baseResult;
		} catch (Exception e) {
			logger.error("TeaOrderController getOrderInfo error:" + e.getMessage());
			logger.error("TeaOrderController getOrderInfo end.");
			return BaseResult.exception(e.getMessage());
		}
	}
	
	@RequestMapping(value="/saveOrder", method = RequestMethod.POST)
	public @ResponseBody BaseResult saveOrder(HttpServletRequest req,@RequestBody TeaOrder order){
		logger.info("TeaOrderController  saveOrder begin.");
		try {
			logger.info("saveOrder begin.");
			BaseResult baseResult = teaOrderService.saveOrder(order);
			logger.info("saveOrder end.");
			logger.info("TeaOrderController saveOrder end.");
			return baseResult;
		} catch (Exception e) {
			logger.error("TeaOrderController saveOrder error:" + e.getMessage());
			logger.error("TeaOrderController saveOrder end.");
			return BaseResult.exception(e.getMessage());
		}
	}
	
	@RequestMapping(value="/cancleOrder", method = RequestMethod.GET)
	public @ResponseBody BaseResult cancleOrder(HttpServletRequest req){
		logger.info("TeaOrderController  cancleOrder begin.");
		try {
			logger.info("cancleOrder begin.");
			String orderId = req.getParameter("orderId");
			String status = req.getParameter("status");
			BaseResult baseResult = teaOrderService.cancleOrder(orderId,status);
			logger.info("cancleOrder end.");
			logger.info("TeaOrderController cancleOrder end.");
			return baseResult;
		} catch (Exception e) {
			logger.error("TeaOrderController cancleOrder error:" + e.getMessage());
			logger.error("TeaOrderController cancleOrder end.");
			return BaseResult.exception(e.getMessage());
		}
	}
	
	/**
	 * 根据用户id查询订单信息
	 * @param req
	 * @return
	 */
	@RequestMapping(value="/getOrderListByUserId", method = {RequestMethod.GET, RequestMethod.POST})
	public @ResponseBody BaseResult getOrderListByUserId(HttpServletRequest req, TeaOrder teaOrder, PaginationInfo paginationInfo){
		logger.info("TeaOrderController  getOrderListByUserId begin.");
		try {
			String userid = req.getParameter("userId");
			String status = req.getParameter("status");
			HashMap<String, String> param = new HashMap<String, String>();
			param.put("userid", userid);
			param.put("status", status);
			logger.info("getOrderListByUserId begin.");
			BaseResult baseResult = teaOrderService.getOrderListByUserId(param, teaOrder, paginationInfo);
			logger.info("getOrderListByUserId end.");
			logger.info("TeaOrderController getOrderListByUserId end.");
			return baseResult;
		} catch (Exception e) {
			logger.error("TeaOrderController getOrderListByUserId error:" + e.getMessage());
			logger.error("TeaOrderController getOrderListByUserId end.");
			return BaseResult.exception(e.getMessage());
		}
	}
	
	/**
	 * 卖家发货
	 * @param req
	 * @return
	 */
	@RequestMapping(value="/sendOrder", method = RequestMethod.POST)
	public @ResponseBody BaseResult sendOrder(HttpServletRequest req, TeaOrder teaOrder){
		logger.info("TeaOrderController  sendOrder begin.");
		try {
			logger.info("sendOrder begin.");
			String orderId = req.getParameter("orderId");
			String wayNo = req.getParameter("wayNo");
			BaseResult baseResult = teaOrderService.sendOrder(teaOrder);
			logger.info("sendOrder end.");
			logger.info("TeaOrderController sendOrder end.");
			return baseResult;
		} catch (Exception e) {
			logger.error("TeaOrderController sendOrder error:" + e.getMessage());
			logger.error("TeaOrderController sendOrder end.");
			return BaseResult.exception(e.getMessage());
		}
	}

}
